//
//  ContentView.swift
//  testc
//
//  Created by Ares Cheuk on 2024/3/30.
//

import SwiftUI
import RealityKit
import RealityKitContent

struct ContentView: View {

    @State private var showImmersiveSpace = false
    @State private var immersiveSpaceIsShown = false

    ///控制显示
    @State  var isRootView = false
    
    ///列表
    @State private var confirmationShown = false
    @State private var confirmationBackShown = false
    let notificationName = "CcNotificationDeleteAll"

    ///首页
    @State var finalTitle : String = "Hello 随意"
    @State var isTitleFinished: Bool = false
    
    
    @Environment(\.openImmersiveSpace) var openImmersiveSpace
    @Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace

    var body: some View {
        
        NavigationStack{
            if(isRootView){
                ShowListView()
                    .toolbar{
                        Button(role: .destructive) {
                            confirmationShown = true
                        } label: {
                            Label("Delete All", systemImage: "trash.fill")
                        }
                        .accessibilityElement()
                        .confirmationDialog(
                            "Are you sure you want to delete all?",
                            isPresented: $confirmationShown,
                            titleVisibility: .visible
                        ) {
                            Text("Are you sure you want to delete all?")
                            Button("Delete All", role: .destructive) {
                                withAnimation {
                                    NotificationCenter.default.post(name: NSNotification.Name(rawValue:notificationName), object: nil, userInfo: nil)
                                }
                            }
                            Button("Continue editing", role: .cancel) {
                            }
                        }
                        
                        Button {
                            confirmationBackShown = true
                        } label: {
                            Label("Back", systemImage: "back.fill")
                        }
                        .accessibilityElement()
                        .confirmationDialog(
                            "Are you sure you want Back and  to delete all?",
                            isPresented: $confirmationBackShown,
                            titleVisibility: .visible
                        ) {
                            Text("Are you sure you want Back and  to delete all?")
                            Button("Back ", role: .destructive) {
                                withAnimation {
                                    
                                    isRootView = false;
                                    showImmersiveSpace = false;
                                    NotificationCenter.default.post(name: NSNotification.Name(rawValue:notificationName), object: nil, userInfo: nil)
                                }
                            }
                            Button("Continue editing", role: .cancel) {
                            }
                        }
                    }
                   
            } else {
                Image("SunSliver")
                    .opacity(isTitleFinished ? 1 : 0)
                    .accessibility(hidden: true)
                    .offset(y: -50)
                VStack {
                    
                    VStack {
                        // A hidden version of the final text keeps the layout fixed
                        // while the overlaid visible version types on.
                        TitleText(title: " Hello 随意 ")
                            .padding(.horizontal, 0)
                            .hidden()
                            .overlay(alignment: .leading) {
                                TitleText(title: finalTitle)
                                    .padding(.leading, 0)
                            }
                        Text("Discover a new way to place furniture")
                            .font(.title)
                            .opacity(isTitleFinished ? 1 : 0)
                    }
                    .padding(.bottom, 50)
                    
//                    
//                    Model3D(named: "Scene", bundle: realityKitContentBundle)
//                        .padding(.bottom, 50)
                                        
                    Toggle("Show ImmersiveSpace", isOn: $showImmersiveSpace)
                        .opacity(isTitleFinished ? 1 : 0)
                        .accessibility(hidden: true)
                        .font(.title)
                        .frame(width: 360)
                        .padding(24)
                        .glassBackgroundEffect()
                    
                    
                }
                .padding()
                .onChange(of: showImmersiveSpace) { _, newValue in
                    Task {
                        
                        isRootView = true
                        
                        if newValue {
                            switch await openImmersiveSpace(id: "ImmersiveSpace") {
                            case .opened:
                                immersiveSpaceIsShown = true
                            case .error, .userCancelled:
                                fallthrough
                            @unknown default:
                                immersiveSpaceIsShown = false
                                showImmersiveSpace = false
                            }
                        } else if immersiveSpaceIsShown {
                            await dismissImmersiveSpace()
                            immersiveSpaceIsShown = false
                        }
                    }
                }
                .typeText(
                    text: $finalTitle,
                    finalText: finalTitle,
                    isFinished: $isTitleFinished,
                    isAnimated: !isTitleFinished)
                .offset(y: 0)
                .frame(maxWidth: 800, maxHeight: 440, alignment: .center)
                .animation(.default.speed(0.25), value: isTitleFinished)

            }
        }
        
      
    }
}

/// The text that displays the app's title.
private struct TitleText: View {
    var title: String
    var body: some View {
        Text(title)
            .monospaced()
            .font(.system(size: 50, weight: .bold))
    }
}


#Preview(windowStyle: .automatic) {
    ContentView()
}
